package nets

import (
	"api/deamon"
	"api/loger"
	"api/service"

	"github.com/gin-gonic/gin"
)

// 方便调试，重新从postgre中拿激活了的课程放到内存中
func flash(c *gin.Context) {
	type authInfo struct {
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData authInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	deamon.ImmediatelyToDo()
}

// 模拟晚上进行的操作
func night(c *gin.Context) {
	type authInfo struct {
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData authInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	deamon.NightToDo()
}

// if you want to shutdown -r but still storage the ram,you can sudo this and quickly reboot
func storageRam(c *gin.Context) {
	type authInfo struct {
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData authInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	deamon.Storage()
}
func getStorageRam(c *gin.Context) {
	type authInfo struct {
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData authInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	deamon.GetStorage()
}
func printRam(c *gin.Context) {
	type authInfo struct {
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData authInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	userCard, ccb := deamon.Print()
	c.JSON(200, gin.H{
		"userCard": userCard,
		"ccb":      ccb,
	})
}
func sudoResume(c *gin.Context) {
	type userInfo struct {
		UserId             int                `json:"user_id"`
		ClassId            int                `json:"class_id"`
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData userInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	if getData.UserId < 0 || getData.ClassId < 0 {
		c.JSON(400, gin.H{"error": "不要试图输入负数"})
		return
	}
	var m service.Message
	m.Resume(getData.ClassId, getData.UserId)
	if m.HaveError {
		c.JSON(400, gin.H{"error": m.Info})
		return
	}
	if !m.IsSuccess {
		c.JSON(400, gin.H{"message": m.Info})
		return
	}
	loger.Loger.Println("sudo:", "管理员操作预约", getData.ClassId, "班级", getData.UserId, "用户")
	c.JSON(200, gin.H{"message": m.Info})
}
func sudoCancelResume(c *gin.Context) {
	type userInfo struct {
		UserId             int                `json:"user_id"`
		ClassId            int                `json:"class_id"`
		SudoAuthentication SudoAuthentication `json:"sudoAuthentication"`
	}
	var getData userInfo
	if err := c.ShouldBindJSON(&getData); err != nil {
		c.JSON(400, gin.H{"error": err.Error()})
		return
	}
	if !authenticateSudo(getData.SudoAuthentication) {
		c.JSON(400, gin.H{"error": "验证失败"})
		return
	}
	var m service.Message
	m.CancelResume(getData.UserId, getData.ClassId)
	if m.HaveError {
		c.JSON(400, gin.H{"error": m.Info})
		return
	}
	if !m.IsSuccess {
		c.JSON(400, gin.H{"message": m.Info})
		return
	}
	loger.Loger.Println("sudo:", "管理员操作取消预约", getData.ClassId, "班级", getData.UserId, "用户")
	c.JSON(200, gin.H{"message": m.Info})
}
